#include <stdio.h>
#include <stdlib.h>
#include "collaboration.h"
#include "MI_server.h"
#include "MIDataAccessor.h"
#include "CORBA_util.h"
#include "Thread.h"

MI_server::MI_server(CORBA::ORB_ptr __orb) : auxiliary::Thread()
{
	orb = __orb;
}

MI_server::~MI_server()
{
	
}

void MI_server::run()
{
	//coger una referencia a rootPOA
	PortableServer::POA_var poa = auxiliary::resolve_init<PortableServer::POA>(orb, "RootPOA");
	
	////////////////////////////////
	//     OBJETOS SERVANTS      //
	///////////////////////////////
	MIDataAccessor_impl* servant = new MIDataAccessor_impl();
	PortableServer::ObjectId_var servant_id = poa->activate_object(servant);
	CORBA::Object_var obj = servant->_this();
	
	//seccion dedicada a asociar un nombre en el NameService al objeto servant
	CosNaming::Name contextName;
	contextName.length(1);
	contextName[0].id   = (const char*) "collaboration";
	contextName[0].kind = (const char*) "";
	
	CosNaming::Name objectName;
	objectName.length(1);
	objectName[0].id   = (const char*) "DataAccessor";
	objectName[0].kind = (const char*) "Object";
	
	if (!auxiliary::bind_object_to_name<CORBA::Object>(orb, obj, contextName, objectName))
		exit(1);
	
	servant->_remove_ref();
	
	//activar manager
	PortableServer::POAManager_var mgr = poa->the_POAManager();
	mgr->activate();
	
	//comenzar a recibir conexiones
	orb->run();
}

